//
//  MainViewController.swift
//  上海微博
//
//  Created by apple on 16/5/11.
//  Copyright © 2016年 kuaikuaipao. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        setupTabbar()
//        print(tabBar.subviews)
    }
    
    //viewDidLoad 只是加载 并不创建控件tabbar子控件
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composeButton)
        //设位置composeButton 的ftame
        let width = tabBar.bounds.width  / CGFloat(childViewControllers.count)-1
        
         // <0 往外扩 >0 往里压
        
        
        composeButton.frame = CGRectInset(tabBar.bounds, width*2, 0)
        
        
        
        
    }
    
    
   //通过extension 把相关代码分组存放
    
    
    //设置tabbar 

    
    
    private func setupTabbar () {
        //这两句代码需联用 显示效果
//        tabBar.backgroundImage = UIImage()
//        tabBar.shadowImage = UIImage ()
        
        tabBar.backgroundImage = UIImage(named:"tabbar_background")
   tabBar.shadowImage = UIImage.cz_singleDotImage(UIColor(white:0.9, alpha: 1.0))
        
         tabBar.addSubview(composeButton)
    }
    
    
 //设置添加撰写按钮
    

    
    private lazy var composeButton: UIButton = {
        
        let button = UIButton ()
        
      let imageName = "tabbar_compose_icon_add"
       button.setImage(UIImage(named: imageName), forState:.Normal )
        
      button.setImage(UIImage(named: imageName + "_highlighted"), forState:.Highlighted)
        //
        let backImageName = "tabbar_compose_button"
      button.setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
      button.setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        
       button.sizeToFit()
        
        return button
        
        
    }( )
    

    
    

    private func addChildViewControllers (){
        
        addChildViewController(HomeViewController (), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController (), title: "消息", imageName: "tabbar_message_center")
        
        
        addChildViewController(UIViewController ())
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
       
    
        
        private func addChildViewController(vc:UIViewController, title: String ,imageName: String) {
            
            //title 从内向外设置
            
            vc.title = title
            //文字的默认和选中状态
            vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
            
            vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(10)], forState: .Normal)
            //图片
           vc.tabBarItem.image = UIImage (named:imageName)
        vc.tabBarItem.selectedImage = UIImage (named: imageName+"_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
           
        let nav = UINavigationController(rootViewController: vc)
        
        //添加子控制器
            addChildViewController(nav)
        
        
    }

    
        
        
    }
    
    
    
// 第一部分内容 疑问shadowImage  添加按钮懒加载的写法和格式  extension 用法



    
    
    
    
    
    

   

